// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey.layouts;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */

import com.incrediblesoft.redjey.Dimension;
import com.incrediblesoft.redjey.Container;
import com.incrediblesoft.redjey.Component;

public class GridLayout implements Layout
{
    int rows;
    int cols;

    public GridLayout()
    {
        this(1, 0);
    }

    public GridLayout(int rows, int cols)
    {
        this.rows = rows;
        this.cols = cols;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getColumns() {
        return cols;
    }

    public void setColumns(int cols) {
        this.cols = cols;
    }

    public Dimension preferredLayoutSize(Container parent)
    {
        int ncomponents = parent.getComponentCount();
        int nrows = rows;
        int ncols = cols;

        if (ncols > 0)
            nrows = (ncomponents + ncols - 1) / ncols;
        else
            ncols = (ncomponents + nrows - 1) / nrows;

        int w = 0;
        int h = 0;

        for (int i = 0 ; i < ncomponents ; i++)
        {
            Component comp = parent.getComponent(i);
            Dimension d = comp.getPreferredSize();
            if (w < d.width)
                w = d.width;
            if (h < d.height)
                h = d.height;
        }

        //System.out.println("GridLayout->preferredSize("+(ncols*w + (ncols-1))+", "+ (nrows*h + (nrows-1))+")");
        return new Dimension(ncols*w + (ncols-1), nrows*h + (nrows-1));
    }

    public void layoutContainer(Container parent)
    {
        //Insets insets = parent.getInsets();
        int ncomponents = parent.getComponentCount();
        int nrows = rows;
        int ncols = cols;
        int hgap = parent.getCurrentStyle().getPaddingHorizontal();
        int vgap = parent.getCurrentStyle().getPaddingVertical();
        int[] insets = parent.getInsets();

        if (ncomponents == 0)
            return;

        if (ncols > 0)
            nrows = (ncomponents + ncols - 1) / ncols;
        else
            ncols = (ncomponents + nrows - 1) / nrows;

        Dimension clientSize = parent.getClientSize();
        int w = clientSize.width / ncols;
        int h = clientSize.height / nrows;

        for (int c = 0, x = 0 ; c < ncols ; c++, x += w + hgap)
        {
            for (int r = 0, y = 0 ; r < nrows ; r++, y += h + vgap)
            {
                int i = r * ncols + c;
                if (i < ncomponents)
                    parent.getComponent(i).setBounds(x + insets[0], y + insets[2], w, h);
            }
        }
    }

}
